Imports System.Runtime.InteropServices
Imports System.Reflection
Imports System.Drawing
Imports System.Threading

Module Keyboard
    Public Declare Function UnhookWindowsHookEx Lib "user32" _
      (ByVal hHook As Integer) As Integer

    Public Declare Function SetWindowsHookEx Lib "user32" _
      Alias "SetWindowsHookExA" (ByVal idHook As Integer, _
      ByVal lpfn As KeyboardHookDelegate, ByVal hmod As Integer, _
      ByVal dwThreadId As Integer) As Integer

    Private Declare Function GetAsyncKeyState Lib "user32" _
      (ByVal vKey As Integer) As Integer

    Private Declare Function CallNextHookEx Lib "user32" _
      (ByVal hHook As Integer, _
      ByVal nCode As Integer, _
      ByVal wParam As Integer, _
      ByVal lParam As KBDLLHOOKSTRUCT) As Integer

    Private Declare Sub keybd_event Lib "user32.dll" _
      (ByVal bVk As Byte, _
      ByVal bScan As Byte, _
      ByVal dwFlags As Long, _
      ByVal dwExtraInfo As Long)

    Private Declare Function MapVirtualKey Lib "user32.dll" Alias "MapVirtualKeyA" _
      (ByVal uCode As Integer, _
      ByVal uMapType As Integer) As Integer

    Declare Sub mouse_event Lib "user32" Alias "mouse_event" _
    (ByVal dwFlags As Long, _
    ByVal dx As Long, _
    ByVal dy As Long, _
    ByVal cButtons As Long, _
    ByVal dwExtraInfo As Long)

    Const MOUSEEVENTF_MOVE As Int32 = &H1 '  mouse move
    Const MOUSEEVENTF_LEFTDOWN As Int32 = &H2 '  left button down
    Const MOUSEEVENTF_LEFTUP As Int32 = &H4 '  left button up
    Const MOUSEEVENTF_RIGHTDOWN As Int32 = &H8 '  right button down
    Const MOUSEEVENTF_RIGHTUP As Int32 = &H10 '  right button up
    Const MOUSEEVENTF_MIDDLEDOWN As Int32 = &H20 '  middle button down
    Const MOUSEEVENTF_MIDDLEUP As Int32 = &H40 '  middle button up
    Const MOUSEEVENTF_ABSOLUTE As Int32 = &H8000 '  absolute move
    Const MOUSEEVENTF_WHEEL As Int32 = &H800 ' wheel button rolled

    Public Structure KBDLLHOOKSTRUCT
        Public vkCode As Integer
        Public scanCode As Integer
        Public flags As Integer
        Public time As Integer
        Public dwExtraInfo As Integer
    End Structure

    ' Low-Level Keyboard Constants
    Private Const HC_ACTION As Integer = 0
    Private Const LLKHF_EXTENDED As Integer = &H1
    Private Const LLKHF_INJECTED As Integer = &H10
    Private Const LLKHF_ALTDOWN As Integer = &H20
    Private Const LLKHF_UP As Integer = &H80

    ' Virtual Keys
    Public Const VK_TAB = &H9
    Public Const VK_CONTROL = &H11
    Public Const VK_ESCAPE = &H1B
    Public Const VK_DELETE = &H2E

    Private Const WH_KEYBOARD_LL As Integer = 13&
    Public KeyboardHandle As Integer


    ' Implement this function to block as many
    ' key combinations as you'd like
    Public Function IsHooked( _
      ByRef Hookstruct As KBDLLHOOKSTRUCT) As Boolean

        Debug.WriteLine("Hookstruct.vkCode: " & Hookstruct.vkCode)
        Debug.WriteLine(Hookstruct.vkCode = VK_ESCAPE)
        Debug.WriteLine(Hookstruct.vkCode = VK_TAB)

        'MsgBox(Form1.ComboBox1.Text)
        If (Hookstruct.vkCode = Form1.vcode(Form1.ComboBox1.SelectedIndex)) Then
            'keybd_event(Form1.vcode(5), MapVirtualKey(Form1.vcode(5), 2), 0, 0)
            keybd_event(&H69, &H49, 0, 0)
            'Call HookedState("Ctrl + Esc blocked")
            Return True
        End If

        Return False
    End Function

    Private Sub HookedState(ByVal Text As String)
        Debug.WriteLine(Text)
        MsgBox(Text)
    End Sub

    Public Function KeyboardCallback(ByVal Code As Integer, _
      ByVal wParam As Integer, _
      ByRef lParam As KBDLLHOOKSTRUCT) As Integer

        Dim temp1 As Int32
        Dim temp2 As Int32
        If (Code = HC_ACTION) Then
            Debug.WriteLine("Calling IsHooked")
            Select Case wParam
                Case 256
                    Select Case (lParam.vkCode)
                        Case Form1.vcode(Form1.ComboBox1.SelectedIndex)
                            temp1 = Form1.vcode(Form1.ComboBox1.SelectedIndex)
                            temp2 = Form1.vcode(Form1.ComboBox2.SelectedIndex)
                        Case Form1.vcode(Form1.ComboBox3.SelectedIndex)
                            temp1 = Form1.vcode(Form1.ComboBox3.SelectedIndex)
                            temp2 = Form1.vcode(Form1.ComboBox4.SelectedIndex)
                        Case Form1.vcode(Form1.ComboBox5.SelectedIndex)
                            temp1 = Form1.vcode(Form1.ComboBox5.SelectedIndex)
                            temp2 = Form1.vcode(Form1.ComboBox6.SelectedIndex)
                        Case Form1.vcode(Form1.ComboBox7.SelectedIndex)
                            temp1 = Form1.vcode(Form1.ComboBox7.SelectedIndex)
                            temp2 = Form1.vcode(Form1.ComboBox8.SelectedIndex)
                        Case Form1.vcode(Form1.ComboBox9.SelectedIndex)
                            temp1 = Form1.vcode(Form1.ComboBox9.SelectedIndex)
                            temp2 = Form1.vcode(Form1.ComboBox10.SelectedIndex)
                        Case Form1.vcode(Form1.ComboBox11.SelectedIndex)
                            temp1 = Form1.vcode(Form1.ComboBox11.SelectedIndex)
                            temp2 = Form1.vcode(Form1.ComboBox12.SelectedIndex)
                        Case Form1.vcode(Form1.ComboBox13.SelectedIndex)
                            temp1 = Form1.vcode(Form1.ComboBox13.SelectedIndex)
                            temp2 = Form1.vcode(Form1.ComboBox14.SelectedIndex)

                    End Select
                Case 257

            End Select

            If (Form1.ComboBox1.SelectedItem <> "" And Form1.ComboBox2.SelectedItem <> "") Then
                If (Form1.ComboBox2.SelectedIndex >= 5 And lParam.vkCode = Form1.vcode(Form1.ComboBox1.SelectedIndex)) Then
                    If (wParam = 256) Then
                        Form1.TextBox1.Text = (lParam.vkCode & "_|_" & Form1.vcode(Form1.ComboBox2.SelectedIndex) & "_|_" & MapVirtualKey(Form1.vcode(Form1.ComboBox2.SelectedIndex), 0))
                        keybd_event(Form1.vcode(Form1.ComboBox2.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox2.SelectedIndex), 0), 0, 0)
                        If Form1.CheckBox8.Checked = True Then
                            keybd_event(Form1.vcode(Form1.ComboBox2.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox2.SelectedIndex), 0), &H2, 0)
                        Else
                            'Thread.Sleep(0)
                        End If
                        If ((Form1.ComboBox1.SelectedItem = Form1.ComboBox3.SelectedItem) Or (Form1.ComboBox1.SelectedItem = Form1.ComboBox5.SelectedItem) Or (Form1.ComboBox1.SelectedItem = Form1.ComboBox7.SelectedItem) Or (Form1.ComboBox1.SelectedItem = Form1.ComboBox9.SelectedItem) Or (Form1.ComboBox1.SelectedItem = Form1.ComboBox11.SelectedItem) Or (Form1.ComboBox1.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    ElseIf (wParam = 257) Then
                        Form1.TextBox1.Text = (lParam.vkCode & "_|up|_" & Form1.vcode(Form1.ComboBox2.SelectedIndex) & "_|_" & MapVirtualKey(Form1.vcode(Form1.ComboBox2.SelectedIndex), 0))
                        keybd_event(Form1.vcode(Form1.ComboBox2.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox2.SelectedIndex), 0), &H2, 0)
                        If (Form1.CheckBox1.Checked = True) Then
                            If Form1.auto1 = 0 Then
                                Form1.auto1 = 1
                            Else
                                Form1.auto1 = 0
                            End If
                        End If
                        If ((Form1.ComboBox1.SelectedItem = Form1.ComboBox3.SelectedItem) Or (Form1.ComboBox1.SelectedItem = Form1.ComboBox5.SelectedItem) Or (Form1.ComboBox1.SelectedItem = Form1.ComboBox7.SelectedItem) Or (Form1.ComboBox1.SelectedItem = Form1.ComboBox9.SelectedItem) Or (Form1.ComboBox1.SelectedItem = Form1.ComboBox11.SelectedItem) Or (Form1.ComboBox1.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    End If
                ElseIf (Form1.ComboBox2.SelectedIndex < 5 And lParam.vkCode = Form1.vcode(Form1.ComboBox1.SelectedIndex)) Then
                    If (Form1.ComboBox2.SelectedIndex = 1) Then
                        temp1 = MOUSEEVENTF_LEFTDOWN
                        temp2 = MOUSEEVENTF_LEFTUP
                    ElseIf (Form1.ComboBox2.SelectedIndex = 2) Then
                        temp1 = MOUSEEVENTF_RIGHTDOWN
                        temp2 = MOUSEEVENTF_RIGHTUP
                    ElseIf (Form1.ComboBox2.SelectedIndex = 4) Then
                        temp1 = MOUSEEVENTF_MIDDLEDOWN
                        temp2 = MOUSEEVENTF_MIDDLEUP
                    End If
                    If (wParam = 256) Then
                        mouse_event(temp1, 0, 0, 0, 0)
                        If Form1.CheckBox8.Checked = True Then
                            mouse_event(temp2, 0, 0, 0, 0)
                        Else
                            'Thread.Sleep(0)
                        End If
                        If ((Form1.ComboBox1.SelectedItem = Form1.ComboBox3.SelectedItem) Or (Form1.ComboBox1.SelectedItem = Form1.ComboBox5.SelectedItem) Or (Form1.ComboBox1.SelectedItem = Form1.ComboBox7.SelectedItem) Or (Form1.ComboBox1.SelectedItem = Form1.ComboBox9.SelectedItem) Or (Form1.ComboBox1.SelectedItem = Form1.ComboBox11.SelectedItem) Or (Form1.ComboBox1.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    ElseIf (wParam = 257) Then
                        mouse_event(temp2, 0, 0, 0, 0)
                        If (Form1.CheckBox1.Checked = True) Then
                            If Form1.auto1 = 0 Then
                                Form1.auto1 = 1
                            Else
                                Form1.auto1 = 0
                            End If
                        End If
                        If ((Form1.ComboBox1.SelectedItem = Form1.ComboBox3.SelectedItem) Or (Form1.ComboBox1.SelectedItem = Form1.ComboBox5.SelectedItem) Or (Form1.ComboBox1.SelectedItem = Form1.ComboBox7.SelectedItem) Or (Form1.ComboBox1.SelectedItem = Form1.ComboBox9.SelectedItem) Or (Form1.ComboBox1.SelectedItem = Form1.ComboBox11.SelectedItem) Or (Form1.ComboBox1.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    End If
                End If
            End If
            If (Form1.ComboBox3.SelectedItem <> "" And Form1.ComboBox4.SelectedItem <> "") Then
                If (Form1.ComboBox4.SelectedIndex >= 5 And lParam.vkCode = Form1.vcode(Form1.ComboBox3.SelectedIndex)) Then
                    If (wParam = 256) Then
                        Form1.TextBox1.Text = (lParam.vkCode & "_|_" & Form1.vcode(Form1.ComboBox4.SelectedIndex) & "_|_" & MapVirtualKey(Form1.vcode(Form1.ComboBox4.SelectedIndex), 0))
                        keybd_event(Form1.vcode(Form1.ComboBox4.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox4.SelectedIndex), 0), 0, 0)
                        If Form1.CheckBox8.Checked = True Then
                            keybd_event(Form1.vcode(Form1.ComboBox4.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox4.SelectedIndex), 0), &H2, 0)
                        Else
                            'Thread.Sleep(0)
                        End If
                        If ((Form1.ComboBox3.SelectedItem = Form1.ComboBox5.SelectedItem) Or (Form1.ComboBox3.SelectedItem = Form1.ComboBox7.SelectedItem) Or (Form1.ComboBox3.SelectedItem = Form1.ComboBox9.SelectedItem) Or (Form1.ComboBox3.SelectedItem = Form1.ComboBox11.SelectedItem) Or (Form1.ComboBox3.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    ElseIf (wParam = 257) Then
                        Form1.TextBox1.Text = (lParam.vkCode & "_|_" & Form1.vcode(Form1.ComboBox4.SelectedIndex) & "_|_" & MapVirtualKey(Form1.vcode(Form1.ComboBox4.SelectedIndex), 0))
                        keybd_event(Form1.vcode(Form1.ComboBox4.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox4.SelectedIndex), 0), &H2, 0)
                        If (Form1.CheckBox2.Checked = True) Then
                            If Form1.auto2 = 0 Then
                                Form1.auto2 = 1
                            Else
                                Form1.auto2 = 0
                            End If
                        End If
                        If ((Form1.ComboBox3.SelectedItem = Form1.ComboBox5.SelectedItem) Or (Form1.ComboBox3.SelectedItem = Form1.ComboBox7.SelectedItem) Or (Form1.ComboBox3.SelectedItem = Form1.ComboBox9.SelectedItem) Or (Form1.ComboBox3.SelectedItem = Form1.ComboBox11.SelectedItem) Or (Form1.ComboBox3.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    End If
                ElseIf (Form1.ComboBox4.SelectedIndex < 5 And lParam.vkCode = Form1.vcode(Form1.ComboBox3.SelectedIndex)) Then
                    If (Form1.ComboBox4.SelectedIndex = 1) Then
                        temp1 = MOUSEEVENTF_LEFTDOWN
                        temp2 = MOUSEEVENTF_LEFTUP
                    ElseIf (Form1.ComboBox4.SelectedIndex = 2) Then
                        temp1 = MOUSEEVENTF_RIGHTDOWN
                        temp2 = MOUSEEVENTF_RIGHTUP
                    ElseIf (Form1.ComboBox4.SelectedIndex = 4) Then
                        temp1 = MOUSEEVENTF_MIDDLEDOWN
                        temp2 = MOUSEEVENTF_MIDDLEUP
                    End If
                    If (wParam = 256) Then
                        mouse_event(temp1, 0, 0, 0, 0)
                        If Form1.CheckBox8.Checked = True Then
                            mouse_event(temp2, 0, 0, 0, 0)
                        Else
                            'Thread.Sleep(0)
                        End If
                        If ((Form1.ComboBox3.SelectedItem = Form1.ComboBox5.SelectedItem) Or (Form1.ComboBox3.SelectedItem = Form1.ComboBox7.SelectedItem) Or (Form1.ComboBox3.SelectedItem = Form1.ComboBox9.SelectedItem) Or (Form1.ComboBox3.SelectedItem = Form1.ComboBox11.SelectedItem) Or (Form1.ComboBox3.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    ElseIf (wParam = 257) Then
                        mouse_event(temp2, 0, 0, 0, 0)
                        If (Form1.CheckBox2.Checked = True) Then
                            If Form1.auto2 = 0 Then
                                Form1.auto2 = 1
                            Else
                                Form1.auto2 = 0
                            End If
                        End If
                        If ((Form1.ComboBox3.SelectedItem = Form1.ComboBox5.SelectedItem) Or (Form1.ComboBox3.SelectedItem = Form1.ComboBox7.SelectedItem) Or (Form1.ComboBox3.SelectedItem = Form1.ComboBox9.SelectedItem) Or (Form1.ComboBox3.SelectedItem = Form1.ComboBox11.SelectedItem) Or (Form1.ComboBox3.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    End If
                End If
            End If
            If (Form1.ComboBox5.SelectedItem <> "" And Form1.ComboBox6.SelectedItem <> "") Then
                If (Form1.ComboBox6.SelectedIndex >= 5 And lParam.vkCode = Form1.vcode(Form1.ComboBox5.SelectedIndex)) Then
                    If (wParam = 256) Then
                        Form1.TextBox1.Text = (lParam.vkCode & "_|_" & Form1.vcode(Form1.ComboBox6.SelectedIndex) & "_|_" & MapVirtualKey(Form1.vcode(Form1.ComboBox6.SelectedIndex), 0))
                        keybd_event(Form1.vcode(Form1.ComboBox6.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox6.SelectedIndex), 0), 0, 0)
                        If Form1.CheckBox8.Checked = True Then
                            keybd_event(Form1.vcode(Form1.ComboBox6.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox6.SelectedIndex), 0), &H2, 0)
                        Else
                            'Thread.Sleep(0)
                        End If
                        If ((Form1.ComboBox5.SelectedItem = Form1.ComboBox7.SelectedItem) Or (Form1.ComboBox5.SelectedItem = Form1.ComboBox9.SelectedItem) Or (Form1.ComboBox5.SelectedItem = Form1.ComboBox11.SelectedItem) Or (Form1.ComboBox5.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    ElseIf (wParam = 257) Then
                        Form1.TextBox1.Text = (lParam.vkCode & "_|_" & Form1.vcode(Form1.ComboBox6.SelectedIndex) & "_|_" & MapVirtualKey(Form1.vcode(Form1.ComboBox6.SelectedIndex), 0))
                        keybd_event(Form1.vcode(Form1.ComboBox6.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox6.SelectedIndex), 0), &H2, 0)
                        If (Form1.CheckBox3.Checked = True) Then
                            If Form1.auto3 = 0 Then
                                Form1.auto3 = 1
                            Else
                                Form1.auto3 = 0
                            End If
                        End If
                        If ((Form1.ComboBox5.SelectedItem = Form1.ComboBox7.SelectedItem) Or (Form1.ComboBox5.SelectedItem = Form1.ComboBox9.SelectedItem) Or (Form1.ComboBox5.SelectedItem = Form1.ComboBox11.SelectedItem) Or (Form1.ComboBox5.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    End If
                ElseIf (Form1.ComboBox6.SelectedIndex < 5 And lParam.vkCode = Form1.vcode(Form1.ComboBox5.SelectedIndex)) Then
                    If (Form1.ComboBox6.SelectedIndex = 1) Then
                        temp1 = MOUSEEVENTF_LEFTDOWN
                        temp2 = MOUSEEVENTF_LEFTUP
                    ElseIf (Form1.ComboBox6.SelectedIndex = 2) Then
                        temp1 = MOUSEEVENTF_RIGHTDOWN
                        temp2 = MOUSEEVENTF_RIGHTUP
                    ElseIf (Form1.ComboBox6.SelectedIndex = 4) Then
                        temp1 = MOUSEEVENTF_MIDDLEDOWN
                        temp2 = MOUSEEVENTF_MIDDLEUP
                    End If
                    If (wParam = 256) Then
                        mouse_event(temp1, 0, 0, 0, 0)
                        If Form1.CheckBox8.Checked = True Then
                            mouse_event(temp2, 0, 0, 0, 0)
                        Else
                            'Thread.Sleep(0)
                        End If
                        If ((Form1.ComboBox5.SelectedItem = Form1.ComboBox7.SelectedItem) Or (Form1.ComboBox5.SelectedItem = Form1.ComboBox9.SelectedItem) Or (Form1.ComboBox5.SelectedItem = Form1.ComboBox11.SelectedItem) Or (Form1.ComboBox5.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    ElseIf (wParam = 257) Then
                        mouse_event(temp2, 0, 0, 0, 0)
                        If (Form1.CheckBox3.Checked = True) Then
                            If Form1.auto3 = 0 Then
                                Form1.auto3 = 1
                            Else
                                Form1.auto3 = 0
                            End If
                        End If
                        If ((Form1.ComboBox5.SelectedItem = Form1.ComboBox7.SelectedItem) Or (Form1.ComboBox5.SelectedItem = Form1.ComboBox9.SelectedItem) Or (Form1.ComboBox5.SelectedItem = Form1.ComboBox11.SelectedItem) Or (Form1.ComboBox5.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    End If
                End If
            End If
            If (Form1.ComboBox7.SelectedItem <> "" And Form1.ComboBox8.SelectedItem <> "") Then
                If (Form1.ComboBox8.SelectedIndex >= 5 And lParam.vkCode = Form1.vcode(Form1.ComboBox7.SelectedIndex)) Then
                    If (wParam = 256) Then
                        Form1.TextBox1.Text = (lParam.vkCode & "_|_" & Form1.vcode(Form1.ComboBox8.SelectedIndex) & "_|_" & MapVirtualKey(Form1.vcode(Form1.ComboBox8.SelectedIndex), 0))
                        keybd_event(Form1.vcode(Form1.ComboBox8.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox8.SelectedIndex), 0), 0, 0)
                        If Form1.CheckBox8.Checked = True Then
                            keybd_event(Form1.vcode(Form1.ComboBox8.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox8.SelectedIndex), 0), &H2, 0)
                        Else
                            'Thread.Sleep(0)
                        End If
                        If ((Form1.ComboBox7.SelectedItem = Form1.ComboBox9.SelectedItem) Or (Form1.ComboBox7.SelectedItem = Form1.ComboBox11.SelectedItem) Or (Form1.ComboBox7.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    ElseIf (wParam = 257) Then
                        Form1.TextBox1.Text = (lParam.vkCode & "_|_" & Form1.vcode(Form1.ComboBox8.SelectedIndex) & "_|_" & MapVirtualKey(Form1.vcode(Form1.ComboBox8.SelectedIndex), 0))
                        keybd_event(Form1.vcode(Form1.ComboBox8.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox8.SelectedIndex), 0), &H2, 0)
                        If (Form1.CheckBox4.Checked = True) Then
                            If Form1.auto4 = 0 Then
                                Form1.auto4 = 1
                            Else
                                Form1.auto4 = 0
                            End If
                        End If
                        If ((Form1.ComboBox7.SelectedItem = Form1.ComboBox9.SelectedItem) Or (Form1.ComboBox7.SelectedItem = Form1.ComboBox11.SelectedItem) Or (Form1.ComboBox7.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    End If
                ElseIf (Form1.ComboBox8.SelectedIndex < 5 And lParam.vkCode = Form1.vcode(Form1.ComboBox7.SelectedIndex)) Then
                    If (Form1.ComboBox8.SelectedIndex = 1) Then
                        temp1 = MOUSEEVENTF_LEFTDOWN
                        temp2 = MOUSEEVENTF_LEFTUP
                    ElseIf (Form1.ComboBox8.SelectedIndex = 2) Then
                        temp1 = MOUSEEVENTF_RIGHTDOWN
                        temp2 = MOUSEEVENTF_RIGHTUP
                    ElseIf (Form1.ComboBox8.SelectedIndex = 4) Then
                        temp1 = MOUSEEVENTF_MIDDLEDOWN
                        temp2 = MOUSEEVENTF_MIDDLEUP
                    End If
                    If (wParam = 256) Then
                        mouse_event(temp1, 0, 0, 0, 0)
                        If Form1.CheckBox8.Checked = True Then
                            mouse_event(temp2, 0, 0, 0, 0)
                        Else
                            'Thread.Sleep(0)
                        End If
                        If ((Form1.ComboBox7.SelectedItem = Form1.ComboBox9.SelectedItem) Or (Form1.ComboBox7.SelectedItem = Form1.ComboBox11.SelectedItem) Or (Form1.ComboBox7.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    ElseIf (wParam = 257) Then
                        mouse_event(temp2, 0, 0, 0, 0)
                        If (Form1.CheckBox4.Checked = True) Then
                            If Form1.auto4 = 0 Then
                                Form1.auto4 = 1
                            Else
                                Form1.auto4 = 0
                            End If
                        End If
                        If ((Form1.ComboBox7.SelectedItem = Form1.ComboBox9.SelectedItem) Or (Form1.ComboBox7.SelectedItem = Form1.ComboBox11.SelectedItem) Or (Form1.ComboBox7.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    End If
                End If
            End If
            If (Form1.ComboBox9.SelectedItem <> "" And Form1.ComboBox10.SelectedItem <> "") Then
                If (Form1.ComboBox10.SelectedIndex >= 5 And lParam.vkCode = Form1.vcode(Form1.ComboBox9.SelectedIndex)) Then
                    If (wParam = 256) Then
                        Form1.TextBox1.Text = (lParam.vkCode & "_|_" & Form1.vcode(Form1.ComboBox10.SelectedIndex) & "_|_" & MapVirtualKey(Form1.vcode(Form1.ComboBox10.SelectedIndex), 0))
                        keybd_event(Form1.vcode(Form1.ComboBox10.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox10.SelectedIndex), 0), 0, 0)
                        If Form1.CheckBox8.Checked = True Then
                            keybd_event(Form1.vcode(Form1.ComboBox10.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox10.SelectedIndex), 0), &H2, 0)
                        Else
                            'Thread.Sleep(0)
                        End If
                        If ((Form1.ComboBox9.SelectedItem = Form1.ComboBox11.SelectedItem) Or (Form1.ComboBox9.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    ElseIf (wParam = 257) Then
                        Form1.TextBox1.Text = (lParam.vkCode & "_|_" & Form1.vcode(Form1.ComboBox10.SelectedIndex) & "_|_" & MapVirtualKey(Form1.vcode(Form1.ComboBox10.SelectedIndex), 0))
                        keybd_event(Form1.vcode(Form1.ComboBox10.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox10.SelectedIndex), 0), &H2, 0)
                        If (Form1.CheckBox5.Checked = True) Then
                            If Form1.auto5 = 0 Then
                                Form1.auto5 = 1
                            Else
                                Form1.auto5 = 0
                            End If
                        End If
                        If ((Form1.ComboBox9.SelectedItem = Form1.ComboBox11.SelectedItem) Or (Form1.ComboBox9.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    End If
                ElseIf (Form1.ComboBox10.SelectedIndex < 5 And lParam.vkCode = Form1.vcode(Form1.ComboBox9.SelectedIndex)) Then
                    If (Form1.ComboBox10.SelectedIndex = 1) Then
                        temp1 = MOUSEEVENTF_LEFTDOWN
                        temp2 = MOUSEEVENTF_LEFTUP
                    ElseIf (Form1.ComboBox10.SelectedIndex = 2) Then
                        temp1 = MOUSEEVENTF_RIGHTDOWN
                        temp2 = MOUSEEVENTF_RIGHTUP
                    ElseIf (Form1.ComboBox10.SelectedIndex = 4) Then
                        temp1 = MOUSEEVENTF_MIDDLEDOWN
                        temp2 = MOUSEEVENTF_MIDDLEUP
                    End If
                    If (wParam = 256) Then
                        mouse_event(temp1, 0, 0, 0, 0)
                        If Form1.CheckBox8.Checked = True Then
                            mouse_event(temp2, 0, 0, 0, 0)
                        Else
                            'Thread.Sleep(0)
                        End If
                        If ((Form1.ComboBox9.SelectedItem = Form1.ComboBox11.SelectedItem) Or (Form1.ComboBox9.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    ElseIf (wParam = 257) Then
                        mouse_event(temp2, 0, 0, 0, 0)
                        If (Form1.CheckBox5.Checked = True) Then
                            If Form1.auto5 = 0 Then
                                Form1.auto5 = 1
                            Else
                                Form1.auto5 = 0
                            End If
                        End If
                        If ((Form1.ComboBox9.SelectedItem = Form1.ComboBox11.SelectedItem) Or (Form1.ComboBox9.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    End If
                End If
            End If
            If (Form1.ComboBox11.SelectedItem <> "" And Form1.ComboBox12.SelectedItem <> "") Then
                If (Form1.ComboBox12.SelectedIndex >= 5 And lParam.vkCode = Form1.vcode(Form1.ComboBox11.SelectedIndex)) Then
                    If (wParam = 256) Then
                        Form1.TextBox1.Text = (lParam.vkCode & "_|_" & Form1.vcode(Form1.ComboBox12.SelectedIndex) & "_|_" & MapVirtualKey(Form1.vcode(Form1.ComboBox12.SelectedIndex), 0))
                        keybd_event(Form1.vcode(Form1.ComboBox12.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox12.SelectedIndex), 0), 0, 0)
                        If Form1.CheckBox8.Checked = True Then
                            keybd_event(Form1.vcode(Form1.ComboBox12.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox12.SelectedIndex), 0), &H2, 0)
                        Else
                            'Thread.Sleep(0)
                        End If
                        If ((Form1.ComboBox11.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    ElseIf (wParam = 257) Then
                        Form1.TextBox1.Text = (lParam.vkCode & "_|_" & Form1.vcode(Form1.ComboBox12.SelectedIndex) & "_|_" & MapVirtualKey(Form1.vcode(Form1.ComboBox12.SelectedIndex), 0))
                        keybd_event(Form1.vcode(Form1.ComboBox12.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox12.SelectedIndex), 0), &H2, 0)
                        If (Form1.CheckBox6.Checked = True) Then
                            If Form1.auto6 = 0 Then
                                Form1.auto6 = 1
                            Else
                                Form1.auto6 = 0
                            End If
                        End If
                        If ((Form1.ComboBox11.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    End If
                ElseIf (Form1.ComboBox12.SelectedIndex < 5 And lParam.vkCode = Form1.vcode(Form1.ComboBox11.SelectedIndex)) Then
                    If (Form1.ComboBox12.SelectedIndex = 1) Then
                        temp1 = MOUSEEVENTF_LEFTDOWN
                        temp2 = MOUSEEVENTF_LEFTUP
                    ElseIf (Form1.ComboBox12.SelectedIndex = 2) Then
                        temp1 = MOUSEEVENTF_RIGHTDOWN
                        temp2 = MOUSEEVENTF_RIGHTUP
                    ElseIf (Form1.ComboBox12.SelectedIndex = 4) Then
                        temp1 = MOUSEEVENTF_MIDDLEDOWN
                        temp2 = MOUSEEVENTF_MIDDLEUP
                    End If
                    If (wParam = 256) Then
                        mouse_event(temp1, 0, 0, 0, 0)
                        If Form1.CheckBox8.Checked = True Then
                            mouse_event(temp2, 0, 0, 0, 0)
                        Else
                            'Thread.Sleep(0)
                        End If
                        If ((Form1.ComboBox11.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    ElseIf (wParam = 257) Then
                        mouse_event(temp2, 0, 0, 0, 0)
                        If (Form1.CheckBox6.Checked = True) Then
                            If Form1.auto6 = 0 Then
                                Form1.auto6 = 1
                            Else
                                Form1.auto6 = 0
                            End If
                        End If
                        If ((Form1.ComboBox11.SelectedItem = Form1.ComboBox13.SelectedItem)) Then
                        Else
                            Return 1
                        End If
                    End If
                End If
            End If
            If (Form1.ComboBox13.SelectedItem <> "" And Form1.ComboBox14.SelectedItem <> "") Then
                If (Form1.ComboBox14.SelectedIndex >= 5 And lParam.vkCode = Form1.vcode(Form1.ComboBox13.SelectedIndex)) Then
                    If (wParam = 256) Then
                        Form1.TextBox1.Text = (lParam.vkCode & "_|_" & Form1.vcode(Form1.ComboBox14.SelectedIndex) & "_|_" & MapVirtualKey(Form1.vcode(Form1.ComboBox14.SelectedIndex), 0))
                        keybd_event(Form1.vcode(Form1.ComboBox14.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox14.SelectedIndex), 0), 0, 0)
                        If Form1.CheckBox8.Checked = True Then
                            keybd_event(Form1.vcode(Form1.ComboBox14.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox14.SelectedIndex), 0), &H2, 0)
                        Else
                            'Thread.Sleep(0)
                        End If
                        Return 1
                    ElseIf (wParam = 257) Then
                        Form1.TextBox1.Text = (lParam.vkCode & "_|_" & Form1.vcode(Form1.ComboBox14.SelectedIndex) & "_|_" & MapVirtualKey(Form1.vcode(Form1.ComboBox14.SelectedIndex), 0))
                        keybd_event(Form1.vcode(Form1.ComboBox14.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox14.SelectedIndex), 0), &H2, 0)
                        If (Form1.CheckBox7.Checked = True) Then
                            If Form1.auto7 = 0 Then
                                Form1.auto7 = 1
                            Else
                                Form1.auto7 = 0
                            End If
                        End If
                        Return 1
                    End If
                ElseIf (Form1.ComboBox14.SelectedIndex < 5 And lParam.vkCode = Form1.vcode(Form1.ComboBox13.SelectedIndex)) Then
                    If (Form1.ComboBox14.SelectedIndex = 1) Then
                        temp1 = MOUSEEVENTF_LEFTDOWN
                        temp2 = MOUSEEVENTF_LEFTUP
                    ElseIf (Form1.ComboBox14.SelectedIndex = 2) Then
                        temp1 = MOUSEEVENTF_RIGHTDOWN
                        temp2 = MOUSEEVENTF_RIGHTUP
                    ElseIf (Form1.ComboBox14.SelectedIndex = 4) Then
                        temp1 = MOUSEEVENTF_MIDDLEDOWN
                        temp2 = MOUSEEVENTF_MIDDLEUP
                    End If
                    If (wParam = 256) Then
                        mouse_event(temp1, 0, 0, 0, 0)
                        If Form1.CheckBox8.Checked = True Then
                            mouse_event(temp2, 0, 0, 0, 0)
                        Else
                            'Thread.Sleep(0)
                        End If
                        Return 1
                    ElseIf (wParam = 257) Then
                        mouse_event(temp2, 0, 0, 0, 0)
                        If (Form1.CheckBox7.Checked = True) Then
                            If Form1.auto7 = 0 Then
                                Form1.auto7 = 1
                            Else
                                Form1.auto7 = 0
                            End If
                        End If
                        Return 1
                    End If
                    'mouse_event(MOUSEEVENTF_RIGHTDOWN + MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
                End If
            End If
            'If (IsHooked(lParam)) Then
            'MsgBox(wParam)
            'Return 1
            'End If
        End If

        Return CallNextHookEx(KeyboardHandle, Code, wParam, lParam)

    End Function


    Public Delegate Function KeyboardHookDelegate( _
      ByVal Code As Integer, _
      ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) _
                   As Integer

    <MarshalAs(UnmanagedType.FunctionPtr)> _
    Private callback As KeyboardHookDelegate

    Public Sub HookKeyboard()
        'MsgBox("hookkeyboard")
        callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)

        KeyboardHandle = SetWindowsHookEx( _
          WH_KEYBOARD_LL, callback, _
          Marshal.GetHINSTANCE( _
          [Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)

        Call CheckHooked()
    End Sub

    Public Sub CheckHooked()
        If (Hooked()) Then
            'MsgBox("Keyboard hooked")
            Debug.WriteLine("Keyboard hooked")
        Else
            'MsgBox("Keyboard hook failed: " & Err.LastDllError)
            Debug.WriteLine("Keyboard hook failed: " & Err.LastDllError)
        End If
    End Sub

    Private Function Hooked()
        Hooked = KeyboardHandle <> 0
    End Function

    Public Sub UnhookKeyboard()
        If (Hooked()) Then
            Call UnhookWindowsHookEx(KeyboardHandle)
        End If
    End Sub

    Public Sub presskey(ByVal i As Integer)
        If i = 1 And Form1.auto1 = 1 Then
            keybd_event(Form1.vcode(Form1.ComboBox2.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox2.SelectedIndex), 0), 0, 0)
            Thread.Sleep(Form1.TextBox3.Text)
            keybd_event(Form1.vcode(Form1.ComboBox2.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox2.SelectedIndex), 0), &H2, 0)
        End If
        If i = 2 And Form1.auto2 = 1 Then
            keybd_event(Form1.vcode(Form1.ComboBox4.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox4.SelectedIndex), 0), 0, 0)
            Thread.Sleep(Form1.TextBox3.Text)
            keybd_event(Form1.vcode(Form1.ComboBox4.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox4.SelectedIndex), 0), &H2, 0)
        End If
        If i = 3 And Form1.auto3 = 1 Then
            keybd_event(Form1.vcode(Form1.ComboBox6.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox6.SelectedIndex), 0), 0, 0)
            Thread.Sleep(Form1.TextBox3.Text)
            keybd_event(Form1.vcode(Form1.ComboBox6.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox6.SelectedIndex), 0), &H2, 0)
        End If
        If i = 4 And Form1.auto4 = 1 Then
            keybd_event(Form1.vcode(Form1.ComboBox8.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox8.SelectedIndex), 0), 0, 0)
            Thread.Sleep(Form1.TextBox3.Text)
            keybd_event(Form1.vcode(Form1.ComboBox8.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox8.SelectedIndex), 0), &H2, 0)
        End If
        If i = 5 And Form1.auto5 = 1 Then
            keybd_event(Form1.vcode(Form1.ComboBox10.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox10.SelectedIndex), 0), 0, 0)
            Thread.Sleep(Form1.TextBox3.Text)
            keybd_event(Form1.vcode(Form1.ComboBox10.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox10.SelectedIndex), 0), &H2, 0)
        End If
        If i = 6 And Form1.auto6 = 1 Then
            keybd_event(Form1.vcode(Form1.ComboBox12.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox12.SelectedIndex), 0), 0, 0)
            Thread.Sleep(Form1.TextBox3.Text)
            keybd_event(Form1.vcode(Form1.ComboBox12.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox12.SelectedIndex), 0), &H2, 0)
        End If
        If i = 7 And Form1.auto7 = 1 Then
            keybd_event(Form1.vcode(Form1.ComboBox14.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox14.SelectedIndex), 0), 0, 0)
            Thread.Sleep(Form1.TextBox3.Text)
            keybd_event(Form1.vcode(Form1.ComboBox14.SelectedIndex), MapVirtualKey(Form1.vcode(Form1.ComboBox14.SelectedIndex), 0), &H2, 0)
        End If
    End Sub

End Module